namespace DotNetNuke.Services.Log.SiteLog
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Security;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Threading;

    public class SiteLogController
    {
        public void AddSiteLog(int PortalId, int UserId, string Referrer, string URL, string UserAgent, string UserHostAddress, string UserHostName, int TabId, int AffiliateId, int SiteLogBuffer, string SiteLogStorage)
        {
            PortalSecurity objSecurity = new PortalSecurity();
            try
            {
                if (DotNetNuke.Common.Globals.PerformanceSetting == DotNetNuke.Common.Globals.PerformanceSettings.NoCaching)
                {
                    SiteLogBuffer = 1;
                }
                switch (SiteLogBuffer)
                {
                    case 0:
                        return;

                    case 1:
                        switch (SiteLogStorage)
                        {
                            case "D":
                                DataProvider.Instance().AddSiteLog(DateAndTime.Now, PortalId, UserId, objSecurity.InputFilter(Referrer, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(URL, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(UserAgent, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(UserHostAddress, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(UserHostName, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), TabId, AffiliateId);
                                break;

                            case "F":
                                this.W3CExtendedLog(DateAndTime.Now, PortalId, UserId, objSecurity.InputFilter(Referrer, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(URL, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(UserAgent, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(UserHostAddress, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), objSecurity.InputFilter(UserHostName, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup), TabId, AffiliateId);
                                break;
                        }
                        return;
                }
                string key = "SiteLog" + PortalId.ToString();
                if (DataCache.GetCache(key) == null)
                {
                    DataCache.SetCache(key, new ArrayList());
                }
                ArrayList arrSiteLog = (ArrayList) DataCache.GetCache(key);
                SiteLogInfo objSiteLog = new SiteLogInfo();
                objSiteLog.DateTime = DateAndTime.Now;
                objSiteLog.PortalId = PortalId;
                objSiteLog.UserId = UserId;
                objSiteLog.Referrer = objSecurity.InputFilter(Referrer, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup);
                objSiteLog.URL = objSecurity.InputFilter(URL, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup);
                objSiteLog.UserAgent = objSecurity.InputFilter(UserAgent, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup);
                objSiteLog.UserHostAddress = objSecurity.InputFilter(UserHostAddress, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup);
                objSiteLog.UserHostName = objSecurity.InputFilter(UserHostName, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup);
                objSiteLog.TabId = TabId;
                objSiteLog.AffiliateId = AffiliateId;
                arrSiteLog.Add(objSiteLog);
                if (arrSiteLog.Count >= SiteLogBuffer)
                {
                    BufferedSiteLog objBufferedSiteLog = new BufferedSiteLog();
                    objBufferedSiteLog.SiteLogStorage = SiteLogStorage;
                    objBufferedSiteLog.SiteLog = arrSiteLog;
                    DataCache.RemoveCache(key);
                    new Thread(new ThreadStart(objBufferedSiteLog.AddSiteLog)).Start();
                }
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                
            }
        }

        public void DeleteSiteLog(DateTime DateTime, int PortalId)
        {
            DataProvider.Instance().DeleteSiteLog(DateTime, PortalId);
        }

        public IDataReader GetSiteLog(int PortalId, string PortalAlias, int ReportType, DateTime StartDate, DateTime EndDate)
        {
            return DataProvider.Instance().GetSiteLog(PortalId, PortalAlias, "GetSiteLog" + ReportType.ToString(), StartDate, EndDate);
        }

        public void W3CExtendedLog(DateTime DateTime, int PortalId, int UserId, string Referrer, string URL, string UserAgent, string UserHostAddress, string UserHostName, int TabId, int AffiliateId)
        {
            StreamWriter objStream;
            string LogFilePath = DotNetNuke.Common.Globals.ApplicationMapPath + @"\Portals\" + PortalId.ToString() + @"\Logs\";
            string LogFileName = "ex" + DateAndTime.Now.ToString("yyMMdd") + ".log";
            if (!File.Exists(LogFilePath + LogFileName))
            {
                try
                {
                    Directory.CreateDirectory(LogFilePath);
                    objStream = File.AppendText(LogFilePath + LogFileName);
                    objStream.WriteLine("#Software: Microsoft Internet Information Services 6.0");
                    objStream.WriteLine("#Version: 1.0");
                    objStream.WriteLine("#Date: " + DateAndTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                    objStream.WriteLine("#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status");
                    objStream.Flush();
                    objStream.Close();
                }
                catch (Exception exception1)
                {
                    
                    
                }
            }
            try
            {
                objStream = File.AppendText(LogFilePath + LogFileName);
                StringBuilder objStringBuilder = new StringBuilder(0x400);
                objStringBuilder.Append(DateTime.ToString("yyyy-MM-dd hh:mm:ss") + " ");
                objStringBuilder.Append(UserHostAddress + " ");
                objStringBuilder.Append("GET ");
                objStringBuilder.Append(URL + " ");
                objStringBuilder.Append("- ");
                objStringBuilder.Append("80 ");
                objStringBuilder.Append("- ");
                objStringBuilder.Append(UserAgent + " ");
                objStringBuilder.Append("200 ");
                objStringBuilder.Append("0 ");
                objStringBuilder.Append("0");
                objStream.WriteLine(objStringBuilder.ToString());
                objStream.Flush();
                objStream.Close();
            }
            catch (Exception exception2)
            {
                ProjectData.SetProjectError(exception2);
                
            }
        }
    }
}

